/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.sgf.dt.provider.service;

import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import com.inspur.edp.lcm.metadata.api.entity.uri.MetadataURI;
import com.inspur.edp.sgf.api.common.ResourceType;
import com.inspur.edp.sgf.api.common.SummaryInfo;
import com.inspur.edp.sgf.api.entity.ServiceOperation;
import com.inspur.edp.sgf.dt.provider.common.EapiBuilder;
import com.inspur.edp.sgf.dt.provider.utils.MetadataUtils;
import com.inspur.edp.sgf.dt.spi.EapiProvider;
import com.inspur.edp.sgf.dt.spi.ResourceProvider;
import com.inspur.edp.sgf.provider.vo.utils.ViewModelUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能描述: 高级型视图对象元数据设计时定义适配器
 *
 * @author Fynn Qi
 * @author Kaixuan Shi
 * @version V1.1
 * @since 2020/2/3 10:21
 */
@EapiProvider(resourceType = "VO")
public class ViewModelProvider implements ResourceProvider {

    @Override
    public List<SummaryInfo> getSummaryOperations(MetadataURI targetMdURI, MetadataURI sourceMdURI, String projectPath) {
        GspMetadata metadata = MetadataUtils.getDtVoMetadata(targetMdURI, sourceMdURI, projectPath);
        return ViewModelUtils.getAllSummaryOperations(metadata);
    }

    @Override
    public List<ServiceOperation> getOperations(MetadataURI targetMdURI, MetadataURI sourceMdURI, String projectPath) {
        GspMetadata metadata = MetadataUtils.getDtVoMetadata(targetMdURI, sourceMdURI, projectPath);
        return ViewModelUtils.getAllOperations(metadata);
    }

    @Override
    public List<ServiceOperation> getOperations(MetadataURI targetMdURI, MetadataURI sourceMdURI, String projectPath, List<String> operationIds) {
        if (operationIds == null || operationIds.isEmpty()) {
            return new ArrayList<>();
        }
        GspMetadata metadata = MetadataUtils.getDtVoMetadata(targetMdURI, sourceMdURI, projectPath);
        List<ServiceOperation> operations = ViewModelUtils.getAllOperations(metadata);
        // 返回过滤的服务操作
        return operations.stream().filter(x -> operationIds.stream().anyMatch(id -> id.equals(x.getId()))).collect(Collectors.toList());
    }

    @Override
    public GspMetadata create(GspMetadata metadata, String path) {
        EapiBuilder builder = new EapiBuilder();
        return builder.buildEapi(metadata, path, ResourceType.VO_ADVANCE);
    }

    @Override
    public GspMetadata create(GspMetadata metadata) {
        EapiBuilder builder = new EapiBuilder();
        return builder.buildEapi(metadata, ResourceType.VO_ADVANCE);
    }

    @Override
    public GspMetadata update(String eapiMetadataId, GspMetadata metadata) {
        EapiBuilder builder = new EapiBuilder();
        return builder.updateEapi(eapiMetadataId, metadata, ResourceType.VO_ADVANCE);
    }

}
